﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.ObjectFind
{
    class B1Form_FindResultForm : SAPBoAddon.B1Form
    {
        #region properties
        #endregion
        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            this.FormSetting.FormType = "AVA_FindResult";
            this.FormSetting.FormFileName = "AVA_FindResultForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {

        }
        #endregion
        #region constructor
        public B1Form_FindResultForm()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_FindResultForm_frm_ItemEvent);
        }

        void B1Form_FindResultForm_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (!pVal.BeforeAction)
            {
                if (pVal.ItemUID == "Grid" && pVal.Row >= 0 && pVal.EventType == SAPbouiCOM.BoEventTypes.et_CLICK)
                {
                    SAPbouiCOM.Grid oGrid = CurrentFormEx.Items.Item("Grid").Specific;
                    oGrid.Rows.SelectedRows.Add(pVal.Row);
                }
                if (pVal.ItemUID == "Grid" && pVal.Row >= 0 && pVal.EventType == SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK)
                {
                    SAPbouiCOM.Grid oGrid = CurrentFormEx.Items.Item("Grid").Specific;
                    oGrid.Rows.SelectedRows.Add(pVal.Row);
                    OpenForm(oGrid, pVal.Row);
                }
                if (pVal.ItemUID == "open" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    SAPbouiCOM.Grid oGrid = CurrentFormEx.Items.Item("Grid").Specific;
                    if (oGrid.Rows.SelectedRows.Count > 0)
                    {
                        OpenForm(oGrid, oGrid.Rows.SelectedRows.Item(0, SAPbouiCOM.BoOrderType.ot_RowOrder));
                    }
                }
                if (pVal.ItemUID == "cancel" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                    SBOApp.Forms.ActiveForm.Close();
                if (pVal.ItemUID == "Grid" && pVal.Row == -1 && pVal.EventType == SAPbouiCOM.BoEventTypes.et_DOUBLE_CLICK)
                {

                    SAPbouiCOM.Grid grid = this.CurrentForm.Items.Item("Grid").Specific;
                    if (!String.IsNullOrEmpty(pVal.ColUID))
                        Utils.ReSort(grid, pVal.ColUID);//排序

                }
            }
        }
        #endregion
        #region methods
        private void OpenForm(SAPbouiCOM.Grid oGrid, int row)
        {
            row = oGrid.GetDataTableRowIndex(row);
            string ObjectName = string.Empty;
            ObjectName = oGrid.DataTable.GetValue("Object", row).ToString().Trim();
            if (string.IsNullOrEmpty(ObjectName)) return;
            string DocEntry = oGrid.DataTable.GetValue("DocEntry", row).ToString().Trim();
            switch (ObjectName)
            {
                case "AVA_FF_Clearance":
                case "AVA_FF_AVA_FF_ApplyCustoms":
                    ObjectName = ObjectName + "_BJS";
                    break;
                case "AVA_FF_Storage":
                    ObjectName = ObjectName + "_In_BJS";
                    break;
            }
            this.ShowForm(ObjectName, DocEntry);
        }
        #endregion
    }
}
